using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using AwesomeEnginePC;
using AwesomeEnginePC.Extentions;
using AwesomeEnginePC.Graphics.TwoDimensional.Sprites;
using AwesomeEnginePC.Physics;
using AwesomeEnginePC.Timing;
using AwesomeEnginePC.Inputs;

namespace AE.PC.Test.SpriteTesting
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        BaseSprite simpleSprite;
        SpriteSheet spriteSheet;
        TimeSpan prevTime;

        SpriteSheet sprShePlayNormal;
        SpriteSheet sprShePlayCust;

        SpriteRotator spriteRotator;

        TimingSystem shortTiming;
        TimingSystem longTiming;

        InputManager inputManager;
        bool isSimpleSpriteRed;

        bool isFirstUpdate;

        BasicEffect basicEffect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "AE.Content.ContentCommon";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();

            shortTiming = new TimingSystem();
            longTiming = new TimingSystem();
            isFirstUpdate = true;
            isSimpleSpriteRed = false;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            simpleSprite = new BaseSprite(Content, "poker");
            simpleSprite.ScalingX = 0.5f;
            simpleSprite.ScalingY = 0.5f;
            simpleSprite.Origin = simpleSprite.CenterPoint;
            simpleSprite.Position = new Vector(200, 200);

            spriteSheet = new SpriteSheet(Content, "sprites/spritesheet1", 100, 100);
            spriteSheet.Position = new Vector(300, 300);

            sprShePlayNormal = new SpriteSheet(Content, "sprites/spritesheet1", 100, 100);
            sprShePlayNormal.Position = new Vector(300, 0);
            sprShePlayNormal.Play(new TimeSpan(0, 0, 0, 0, 500), 10, 11, 12);

            sprShePlayCust = new SpriteSheet(Content, "sprites/spritesheet1", 100, 100);
            sprShePlayCust.Position = new Vector(400, 0);
            sprShePlayCust.Play(new TimeSpan(0, 0, 0, 0, 250), new List<int>() { 10,3, 2, 3 });
            sprShePlayCust.Height = 50;
            sprShePlayCust.Width = 100;

            spriteRotator = new SpriteRotator(Content, 200, 200);
            spriteRotator.Position = new Vector(500, 300);
            spriteRotator.Play(new TimeSpan(0, 0, 0, 1, 0), true);
            spriteRotator.Origin = spriteRotator.CenterPoint;
            spriteRotator.Tint = Color.LightBlue;
            spriteRotator.AddImage("ragefaces/angry-desk-flip");
            spriteRotator.AddImage("ragefaces/angry-grinch");
            spriteRotator.AddImage("ragefaces/cat-meh-cat");
            spriteRotator.AddImage("ragefaces/disgusted-impossibru");
            spriteRotator.AddImage("ragefaces/annoyed-mother-of-god");
            spriteRotator.AddImage("ragefaces/laughing-yyyeeeeeaaaaa-sscciiieeenncceee");

            inputManager = new InputManager(true, true, false);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (isFirstUpdate)
            {
                isFirstUpdate = false;
                shortTiming.SetTiming(new TimeSpan(0, 0, 0, 1, 0), gameTime.TotalGameTime);
                longTiming.SetTiming(new TimeSpan(0, 0, 0, 15, 0), gameTime.TotalGameTime);
            }

            if (prevTime == null)
            {
                prevTime = gameTime.TotalGameTime;
            }

            inputManager.Update(gameTime);

            simpleSprite.Update(gameTime);

            if (inputManager.MouseButtonStates[MouseButtons.LeftClick] == ButtonState.Released && inputManager.KeyboardKeyStates[Keys.T] == ButtonState.Released)
            {
                simpleSprite.Rotation += MathHelper.PiOver4 / 60;
            }
            else
            {
                simpleSprite.Rotation -= MathHelper.PiOver4 / 60;
            }

            if (inputManager.JustPressedMouseButtons.Contains(MouseButtons.RightClick) || inputManager.JustPressedKeyboardKeys.Contains(Keys.R))
            {
                isSimpleSpriteRed = !isSimpleSpriteRed;
            }

            simpleSprite.Tint = isSimpleSpriteRed ? Color.Red : Color.White;

            if (shortTiming.HasTargetTimeElapsed(gameTime.TotalGameTime))
            {
                spriteSheet.SequentialFrameChange(1);
            }

            if (longTiming.HasTargetTimeElapsed(gameTime.TotalGameTime))
            {
                sprShePlayNormal.Stop(true);
                sprShePlayCust.Stop();
            }

            sprShePlayNormal.Update(gameTime);
            sprShePlayCust.Update(gameTime);

            spriteRotator.Update(gameTime);
            //spriteRotator.Rotation += MathHelper.PiOver4 / 60;

            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            spriteBatch.Begin();
            simpleSprite.Draw(gameTime, spriteBatch);
            spriteSheet.Draw(gameTime, spriteBatch);
            sprShePlayNormal.Draw(gameTime, spriteBatch);
            sprShePlayCust.Draw(gameTime, spriteBatch);
            spriteRotator.Draw(gameTime, spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
